﻿using System;
using System.Diagnostics;
using System.Security.Cryptography;


namespace Ashwani.Cryptography.Signature
{
    public class SignatureCryptographer
    {
        KeyedHashAlgorithm _key;
        public virtual KeyedHashAlgorithm Key
        {
            get
            {
                return _key;
            }
            set
            {
                _key = value;
            }
        }
        public SignatureCryptographer()
        {
        }

        public SignatureCryptographer(KeyedHashAlgorithm key)
        {
            Key = key;
        }

        public byte[] CreateSignature(byte[] plaintext)
        {
            if (plaintext == null)
                throw new ArgumentNullException("plaintext");
            byte[] hash = Key.ComputeHash(plaintext);
            return hash;
        }



        public bool CompareSignature(byte[] plaintext, byte[] hashedtext)
        {
            if (plaintext == null) throw new ArgumentNullException("plainText");
            if (hashedtext == null) throw new ArgumentNullException("hashedText");
            if (hashedtext.Length == 0) throw new ArgumentException("Array value must be greater than zero.", "hashedText");

            bool result = false;
            byte[] hashedPlainText = null;
            try
            {
                hashedPlainText = CreateSignature(plaintext);
                result = CryptoUtility.CompareBytes(hashedPlainText, hashedtext);
            }
            catch (Exception e)
            {
                throw;
            }
            return result;
        }
    }
}